package com.hhp.hhptt.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hhp.hhptt.utils.ServletUtils;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.hhp.hhptt.service.IPicService;
import com.hhp.hhptt.entity.Pic;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hhp
 * @since 2024-04-01
 */
@RestController
@RequestMapping("/pic")
public class PicController {



@Resource
private IPicService picService;


@PostMapping("/save")
public Boolean save(@RequestBody Pic pic) {
        return picService.saveOrUpdate(pic);
        }

@DeleteMapping("/delete/{id}")
public Boolean delete(@PathVariable Integer id) {
        return picService.removeById(id);
        }
@PostMapping("/deleteBatch")
public Boolean deleteBatch(@RequestBody List<Integer> ids) {
        return picService.removeByIds(ids);
        }

@GetMapping("/getAll")
public List<Pic> findAll() {
        List<Pic> pics = picService.list();
        for(Pic pic:pics){
         pic.setImage(ServletUtils.getImageUrl(pic.getImage()));
        }
        return pics;
        }

@GetMapping("/{id}")
public Pic findOne(@PathVariable Integer id) {
        Pic pic  = picService.getById(id);
        pic.setImage(ServletUtils.getImageUrl(pic.getImage()));
        return pic;
        }



@GetMapping("/page")
public Page<Pic> findPage(@RequestParam Integer pageNum,
@RequestParam Integer pageSize) {

        QueryWrapper<Pic> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        return picService.page(new Page<>(pageNum, pageSize),queryWrapper);
        }
        }

